package com.moyq5.mvc.maker.test.bean.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.Table;

import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;

import com.moyq5.mvc.framework.utils.StringUtils;
import com.moyq5.mvc.maker.test.bean.enums.DeviceStatus;

/**
 * 设备信息
 * @author Moyq5
 * @date 2018年4月13日
 */
@Entity
@Table(name = "iot_device")
@DynamicUpdate
public class IotDevice implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * 设备ID
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")@GenericGenerator(name = "native", strategy = "native")
    @Column(name = "device_id")
	private Long deviceId;
	/**
	 * 品牌
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "brand_id")
	private IotBrand brand;
	/**
	 * 代理商
	 */
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "agent_id")
	private Agent agent;
	/**
	 * 商家
	 */
	@ManyToOne(fetch = FetchType.LAZY, optional = true)
	@JoinColumn(name = "merch_id")
	private Merch merch;
	/**
	 * 门店
	 */
	@ManyToOne(fetch = FetchType.LAZY, optional = true)
	@JoinColumn(name = "shop_id")
	private Shop shop;
	/**
	 * IMEI号
	 */
    @Column(name = "imei", length = 20, nullable = false)
	private String imei;
    /**
	 * 名称
	 */
    @Column(name = "name", length = 100)
	private String name;
    /**
	 * 状态
	 */
    @Enumerated(EnumType.ORDINAL)
    @Column(name = "status", columnDefinition="int(1) default '0'")
	private DeviceStatus status;
    /**
     * 设备扩展信息
     */
    @OneToOne(mappedBy="device", optional = true)
    private IotDeviceExt ext;
    /**
	 * 运行一次所需币数
	 */
	@Column(name = "coin_num", nullable = false, precision = 8, scale = 0, columnDefinition = "int(8) default '1'")
	private Integer coinNum;
    /**
     * 设备二维码内容
     */
    @Column(name = "qrcode", length = 100)
	private String qrcode;
    /**
	 * 最近一次在线时间
	 */
	@Column(name = "on_time", columnDefinition="datetime")
	private Date onTime;
	/**
	 * 入库时间
	 */
	@Column(name = "add_time", nullable = false, columnDefinition="datetime")
	private Date addTime;
	
	@PrePersist
	public void prePersist() {
		if (null == addTime) {
			addTime = new Date();
		}
		if (StringUtils.isEmpty(name)) {
			name = imei; 
		}
		if (null == coinNum) {
			coinNum = 1;
		}
	}
	
	public Long getDeviceId() {
		return deviceId;
	}
	public void setDeviceId(Long deviceId) {
		this.deviceId = deviceId;
	}
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	public Merch getMerch() {
		return merch;
	}
	public void setMerch(Merch merch) {
		this.merch = merch;
	}
	public Shop getShop() {
		return shop;
	}
	public void setShop(Shop shop) {
		this.shop = shop;
	}
	public String getImei() {
		return imei;
	}
	public void setImei(String imei) {
		this.imei = imei;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public DeviceStatus getStatus() {
		return status;
	}
	public void setStatus(DeviceStatus status) {
		this.status = status;
	}
	public Date getOnTime() {
		return onTime;
	}
	public void setOnTime(Date onTime) {
		this.onTime = onTime;
	}
	public Date getAddTime() {
		return addTime;
	}
	public void setAddTime(Date addTime) {
		this.addTime = addTime;
	}
	public String getQrcode() {
		return qrcode;
	}
	public void setQrcode(String qrcode) {
		this.qrcode = qrcode;
	}
	public IotDeviceExt getExt() {
		return ext;
	}
	public void setExt(IotDeviceExt ext) {
		this.ext = ext;
	}
	public IotBrand getBrand() {
		return brand;
	}
	public void setBrand(IotBrand brand) {
		this.brand = brand;
	}
	public Integer getCoinNum() {
		return coinNum;
	}
	public void setCoinNum(Integer coinNum) {
		this.coinNum = coinNum;
	}
	
}
